import time
from datetime import datetime



"""
python时间命令就是三种格式的互相转换
结构时间time.struct_time(*args):一个9个参数的对象，可以转换为元组/列表
时间戳time.time():从1970年开始的计数（微秒）
时间格式format():显示的自定义格式
"""

print('-'*10, 'time模块', '-'*10)
# 获取当前时间
print(time.localtime())


# 时间戳
print(time.time())


# 时间格式
#结构时间<->格式化显示
now = tuple(time.localtime())   # 把时间格式变成元组
print(time.strftime("%Y-%m-%d %H:%M:%S", time.struct_time(now)))    # 把元组格式化显示
print(time.strptime("2020-11-07 16:07:01", "%Y-%m-%d %H:%M:%S"))    # 把格式化转回时间格式（跳过了元组）,夏令时不确定
now1 = (time.struct_time(time.localtime()))
print(now1)


#结构时间<->时间戳
print(time.localtime(time.time()))
print(time.mktime(time.localtime()))


# 格式化显示<->时间戳
timestamp = time.time()
print(time.localtime(timestamp))
print(time.mktime(time.localtime()))



# datetime模块
print('-'*10, 'datetime模块', '-'*10)
print(datetime.today())
print(datetime.today().strftime("%Y-%m-%d %H:%M:%S"))


t = (2020, 11, 5, 22, 14, 17, 3, 310, -1)
new_t = t[:-3]
print(datetime(*new_t))

print(datetime.fromtimestamp(timestamp))


# 通过函数增减日期
from datetime import timedelta


print('-'*10, '函数增减日期', '-'*10)
def get_date(days):
    today1 = datetime.today()
    the_other_day = today1 + timedelta(days=days)
    print('Today is ', today1.strftime("%Y-%m-%d"))
    print('The other day is ', the_other_day.strftime("%Y-%m-%d"))
    return None


get_date(-110)